//
//  CZTabbarController.m
//  YingKeLive
//
//  Created by 西瓜大浪 on 2021/9/2.
//

#import "CZTabbarController.h"
#import "CZTabBar.h"
#import "CZMainViewController.h"
#import "CZMeViewController.h"
#import "CZNavController.h"
#import "UIImage+image.h"
@interface CZTabbarController ()

@end

@implementation CZTabbarController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    //初始化子控制器
    [self initVC];
    //设置子控制器内容
    [self setupAllTitleBUtton];
    //设置tabbar
    [self setupTabBar];
    
    //去除Tabbar 顶部的线
    [[self.tabBarItem standardAppearance] setShadowImage:[UIImage new]];
    [[self.tabBarItem standardAppearance] setBackgroundImage:[UIImage new]];
    
 
//    [self.tabBarItem setBadgeTextAttributes:n forState:UIControlStateNormal]];

}

//只会调用一次
+(void)load{

    //获取全部tabbarItem
    UITabBarItem *item = [UITabBarItem appearance];
        //创建一个描述文本属性的字典
        //字体颜色
        NSDictionary *colorDic =@{
            NSForegroundColorAttributeName:RGB(148, 139, 192),
    
        };
        //字体大小
        NSDictionary *fontDic = @{NSFontAttributeName:[UIFont systemFontOfSize:15]
        };

        [item setTitleTextAttributes:colorDic forState:UIControlStateSelected]; //字体颜色
        [item setTitleTextAttributes:fontDic forState:UIControlStateNormal]; //字体大小
}

//初始化子控制器
-(void)initVC{
    
    CZMainViewController *liveVC = [CZMainViewController new];
    CZNavController *liveNav = [[CZNavController alloc]initWithRootViewController:liveVC];
    
    CZMeViewController *meVC = [CZMeViewController new];
    CZNavController *meNav = [[CZNavController alloc]initWithRootViewController:meVC];
    
    [self addChildViewController:liveNav];
    [self addChildViewController:meNav];

}



//设置tabbar

- (void)setupTabBar{
    
    /**
     
        将系统的tabbar设置为自定义tabbar 当前tabbar为创建好的tabbar 为父类，
        当前tabbar 赋值给了自定义tabbar
        自定义tabbar 就有了当前的属性和方法和已经设置好的
     
    系统的tabbar是readonly 不能被替换
     所以用KVC 来替换
     */

    CZTabBar *tabbar = [[CZTabBar alloc]init];
    //    self.tabBar = tabbar;
    //是直接访问属性赋值的，不是通set方法赋值
    [self setValue:tabbar forKeyPath:@"tabBar"];
    
        

    
}


//设置按钮图片文字
-(void)setupAllTitleBUtton{
    
    
    
    CZNavController *nav1 = self.childViewControllers[0];
    [self setNav:nav1 andTitle:@"广场" andImage:[UIImage imageOriginalWithName:@"tab_live"] andSelImage:[UIImage imageOriginalWithName:@"tab_live_p"]];
    
    CZNavController *nav2 = self.childViewControllers[1];
    [self setNav:nav2 andTitle:@"我的" andImage:[UIImage imageOriginalWithName:@"tab_me"] andSelImage:[UIImage imageOriginalWithName:@"tab_me_p"]];
}

//设置nav 文字图片
- (void)setNav:(UINavigationController *)nav andTitle:(NSString *)title andImage:(UIImage *)img andSelImage:(UIImage *)selImg{
    
    //标题 /默认图片 /高亮图片
    nav.tabBarItem.title = title;
    nav.tabBarItem.image = img;
    nav.tabBarItem.selectedImage =selImg;
    

}
@end
